//package ink.meizi.cloudgateway.conifg;
//
//import com.fasterxml.jackson.core.JsonProcessingException;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import ink.meizi.core.tool.api.R;
//import ink.meizi.core.tool.status.IStatus;
//import ink.meizi.core.tool.status.LoginStatus;
//import ink.meizi.core.tool.utils.JwtParsHander;
//import ink.meizi.platformuserapi.clients.IUserAuthFeignClient;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang.StringUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.context.annotation.PropertySource;
//import org.springframework.core.Ordered;
//import org.springframework.core.io.buffer.DataBuffer;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.stereotype.Component;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Flux;
//import reactor.core.publisher.Mono;
//
//import java.util.List;
//
//
//@Slf4j
//@PropertySource(value = {"classpath:authfilter.properties", "classpath:application.yml"})
//@Component
//public class AuthGlobalFilter implements GlobalFilter, Ordered {
//
//    @Value("#{'${jwt.ignoreUrls}'.split(',')}")
//    private List<String> ignoreUrls;
//    //    @Value("${config.token_name}")
////    private String tokenName;
//    @Autowired
//    private IUserAuthFeignClient userAuthFeignClient;
//
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        ServerHttpRequest request = exchange.getRequest();
//        if (ignoreUrls != null && ignoreUrls.contains(request.getURI().getPath())) {
//            return chain.filter(exchange);
//        }
//        //String access_token = request.getHeaders().getFirst(tokenName);
//        String access_token = JwtParsHander.getToken(request);
//
//
//        if (StringUtils.isBlank(access_token)) {
//            return onError(exchange, LoginStatus.UN_LOGIN);
//        }
//        R r = userAuthFeignClient.validToken(access_token);
//        if (r.getCode() == 200) {
//            ServerHttpRequest shr = request.mutate().header("uid", r.getData().toString()).build();
//            return chain.filter(exchange.mutate().request(shr).build());
//        }
//        return onError(exchange, new IStatus() {
//            @Override
//            public int getCode() {
//                return r.getCode();
//            }
//
//            @Override
//            public String getMsg() {
//                return r.getMsg();
//            }
//        });
//    }
//
//    private Mono<Void> onError(ServerWebExchange exchange, IStatus msg) {
//        ServerHttpResponse response = exchange.getResponse();
//        response.setStatusCode(HttpStatus.UNAUTHORIZED);
//        response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
//        R rd = new R().ofStatus(msg);
//        ObjectMapper objectMapper = new ObjectMapper();
//        String rs = "";
//        try {
//            rs = objectMapper.writeValueAsString(rd);
//        } catch (JsonProcessingException e) {
//            log.error("occur Exception:" + e);
//        }
//        DataBuffer buffer = response.bufferFactory().wrap(rs.getBytes());
//        return response.writeWith(Flux.just(buffer));
//    }
//
//    @Override
//    public int getOrder() {
//        return 0;
//    }
//}
